﻿//
// ParallelGrep
//
// Copyright (C) 2011 - Tatsuya Niioka
//
using System;
using System.Collections.ObjectModel;
using GrepEngine;
using ParallelGrep.Model;
using ParallelGrep.Properties;

namespace ParallelGrep.ViewModel
{
    /// <summary>
    /// 文字コード変換ViewModel
    /// </summary>
    public class CharCodeConvertViewModel : ViewModel
    {
        private MainViewModel m_mainViewModel;

        #region Constructor
        /// <summary>
        /// 指定したMainViewModelで新しい<see cref="CharCodeConvertViewModel"/>クラスを生成します。
        /// </summary>
        /// <param name="mainViewModel">MainViewModel</param>
        public CharCodeConvertViewModel(MainViewModel mainViewModel)
        {
            m_mainViewModel = mainViewModel;
            LoadResources();
        }
        #endregion

        #region LoadResources
        /// <summary>
        /// リソース読み込み処理を実行します。
        /// </summary>
        public void LoadResources()
        {
            #region OutputEncodings
            OutputEncodings.Clear();
            OutputEncodings.Add(new ItemInfo() { ID = "None", Name = Resources.EncodingNone });
            OutputEncodings.Add(new ItemInfo() { ID = "Shift_JIS", Name = Resources.EncodingShiftJIS });
            OutputEncodings.Add(new ItemInfo() { ID = "EUC", Name = Resources.EncodingEUC });
            OutputEncodings.Add(new ItemInfo() { ID = "UTF8", Name = Resources.EncodingUTF8 });
            OutputEncodings.Add(new ItemInfo() { ID = "UTF16LE", Name = Resources.EncodingUTF16LE });
            OutputEncodings.Add(new ItemInfo() { ID = "UTF16BE", Name = Resources.EncodingUTF16BE });

            OnPropertyChanged("OutputEncodingName");
            #endregion

            #region OutputLineEnds
            OutputLineEnds.Clear();
            OutputLineEnds.Add(new ItemInfo() { ID = "None", Name = Resources.LineEndNone });
            OutputLineEnds.Add(new ItemInfo() { ID = "CRLF", Name = Resources.LineEndCRLF });
            OutputLineEnds.Add(new ItemInfo() { ID = "CR", Name = Resources.LineEndCR });
            OutputLineEnds.Add(new ItemInfo() { ID = "LF", Name = Resources.LineEndLF });

            OnPropertyChanged("OutputLineEnd");
            #endregion
        }
        #endregion

        #region AllPropertyChanged
        /// <summary>
        /// すべてのプロパティの変更通知します。
        /// </summary>
        internal void AllPropertyChanged()
        {
            this.OnPropertyChanged("OutputEncodingName");
            this.OnPropertyChanged("OutputLineEnd");
        }
        #endregion

        #region OutputEncodings
        private ObservableCollection<ItemInfo> m_outputEncodings;
        /// <summary>
        /// 出力エンコーディング一覧を取得します。
        /// </summary>
        public ObservableCollection<ItemInfo> OutputEncodings
        {
            get
            {
                if (m_outputEncodings == null)
                {
                    m_outputEncodings = new ObservableCollection<ItemInfo>();
                }
                return m_outputEncodings;
            }
        }
        #endregion

        #region OutputLineEnds
        private ObservableCollection<ItemInfo> m_outputLineEnds;
        /// <summary>
        /// 改行コード一覧を取得します。
        /// </summary>
        public ObservableCollection<ItemInfo> OutputLineEnds
        {
            get
            {
                if (m_outputLineEnds == null)
                {
                    m_outputLineEnds = new ObservableCollection<ItemInfo>();
                }
                return m_outputLineEnds;
            }
        }
        #endregion

        #region OutputEncodingName
        /// <summary>
        /// 出力エンコーディング名を取得または設定します。
        /// </summary>
        public string OutputEncodingName
        {
            get
            {
                return m_mainViewModel.Info.OutputEncodingName.ToString();
            }
            set
            {
                OutputEncodingMode encoding;
                
                try
                {
                    encoding = (OutputEncodingMode)Enum.Parse(typeof(OutputEncodingMode), value);
                }
                catch
                {
                    return;
                }
                if (m_mainViewModel.Info.OutputEncodingName == encoding)
                {
                    return;
                }
                m_mainViewModel.Info.OutputEncodingName = encoding;
                this.OnPropertyChanged("OutputEncodingName");
                m_mainViewModel.IsModified = true;
            }
        }
        #endregion

        #region OutputLineEnd
        /// <summary>
        /// 出力改行コードを取得または設定します。
        /// </summary>
        public string OutputLineEnd
        {
            get
            {
                return m_mainViewModel.Info.OutputLineEnd.ToString();
            }
            set
            {
                OutputLineEnd lineEnd;
                try
                {
                    lineEnd = (OutputLineEnd)Enum.Parse(typeof(OutputLineEnd), value);
                }
                catch
                {
                    return;
                }
                if (m_mainViewModel.Info.OutputLineEnd == lineEnd)
                {
                    return;
                }
                m_mainViewModel.Info.OutputLineEnd = lineEnd;
                this.OnPropertyChanged("OutputLineEnd");
                m_mainViewModel.IsModified = true;
            }
        }
        #endregion

    }
}
